using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using AccessibilityTools.DB;
using SqlSugar;
using System.Text.RegularExpressions;


#region Mantis Code Auto Generate Assistant Illustration V1.0
/// AutoCode 代码生成助手 V1.2
/// 数据库版本 MySQL 5.7.18
/// 设计依据：App.Config所在的MySQL的表格 Fixed_assets_info
/// ********************************************************************
/// <summary>
/// 设计表格默认包含的字段如下：
/// id 主键 int 
/// 表格名称命名规则是小写字母单次，不同单词用下划线连接
/// 字段名称命名规则是小写字母单次，不同单词用下划线连接
/// is_enabled 删除标识 int[ -1 彻底删除][ 0 删除][ 1 有效 ] 
/// 用户登录的函数，关联一个用户登录日志表：user_login(id,user_id,login_time,logout_time),否则log必须为false
/// 时间类型的数据数据库中如果用bigint表达，必须包含 date 或者 time 才能被识别，会自动生成datetime数据
/// 非引用类对象，数字，字符，布尔等类型考虑到数据库，可能为空的情况，如果支持null类型，声明为：类型?
/// 非引用类对象，数字，字符，布尔等类型考虑到数据库，可能为空的情况，如果不支持null类型，声明为：类型
/// 非引用类对象，数字，字符，布尔等类型，如果不支持null类型，在自动转换时候数字会转换为0 字符为 '' 布尔为false
/// 对于数字主键，0是C#中的默认值，故要求数据库中不存在主键值为0的情况
/// 字段和属性为一一对应，属性为简单属性结构，属性Get,Set方法都是public，且未添加其他判断内容
/// 代码生成时间：2024年12月-05日 12时55分
/// <summary>
#endregion

namespace AccessibilityTools.DB
{

    public class Fixed_assets_infoLogic
    {
        Fixed_assets_infoService fixed_assets_infoService = new Fixed_assets_infoService();

        public List<Fixed_assets_info> AllList()
        {
            return fixed_assets_infoService.Query();
        }

        /// <summary>
        /// 增加固定资产记录
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public int AddAssetsInfo(Fixed_assets_info instance)
        {
            instance.IsDeleted = 0;
            return fixed_assets_infoService.Add(instance);
        }

        /// <summary>
        /// 增加固定资产记录
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int AddAssetsInfo(List<Fixed_assets_info> list)
        {
            int count = 0;
            foreach (Fixed_assets_info item in list)
            {
                Fixed_assets_info instance = fixed_assets_infoService.QueryByClauseAsync(x => x.Asset_num == item.Asset_num).Result;
                if (instance != null)
                {
                    bool res = fixed_assets_infoService.UpdateAsync(instance).Result;
                    if (res)
                    {
                        count++;
                    }
                }
                else
                {
                    int res = fixed_assets_infoService.Add(item);
                    count++;
                }

            }
            return count;
            //return fixed_assets_infoService.Add(list);
        }


        /// <summary>
        /// 检测固定资产是否存在
        /// </summary>
        /// <param name="asset_num">资产编号</param>
        /// <returns>返回查询结果 True-存在 False-不存在</returns>
        public bool QueryAssetNumIsExist(string asset_num)
        {
            try
            {
                Fixed_assets_info ins = fixed_assets_infoService.QueryByClauseAsync(x => x.Asset_num == asset_num).Result;
                if (ins != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 创建固定资产
        /// </summary>
        /// <param name="asset_num_fuzzy">模糊查询</param>
        /// <returns></returns>
        public string GenerateAssetNum(string asset_num_fuzzy)
        {
            try
            {
                string pattern = @"\+\d+";
                List<Fixed_assets_info> ins = fixed_assets_infoService.QueryListByClauseAsync($"WHERE asset_num LIKE {asset_num_fuzzy}% order by id desc;").Result;
                if (ins.Any())
                {
                    string[] assetsplit = ins[0].Asset_num.Split('-');
                    if (assetsplit.Length > 2)
                    {
                        string regexNumber = Regex.Match(assetsplit[1], pattern).Value;
                        double serialnum = Convert.ToDouble(regexNumber);
                        string newSerialNumber = ((++serialnum).ToString()).PadLeft(3, '0');
                        return asset_num_fuzzy + "-" + newSerialNumber;
                    }
                    else
                    {
                        return "";
                    }
                }
                return "";

            }
            catch
            {
                return "";
            }
        }


        /// <summary>
        /// 获取固定资产类别
        /// </summary>
        /// <returns></returns>
        public List<string> GetAssetClasses()
        {
            List<Fixed_assets_info> instance = fixed_assets_infoService.QueryListByClauseAsync(f => f.IsDeleted == 0).Result;
            return instance.Select(x => x.Asset_class).Distinct().ToList();
        }

        /// <summary>
        /// 获取使用人
        /// </summary>
        /// <returns></returns>
        public List<string> GetAssetUserName()
        {
            List<Fixed_assets_info> instance = fixed_assets_infoService.QueryListByClauseAsync(f => f.IsDeleted == 0).Result;
            return instance.Select(x => x.User_name).Distinct().ToList();
        }

        /// <summary>
        /// 获取资产名字
        /// </summary>
        /// <returns></returns>
        public List<string> GetAssetName()
        {
            List<Fixed_assets_info> instance = fixed_assets_infoService.QueryListByClauseAsync(f => f.IsDeleted == 0).Result;
            return instance.Select(x => x.Asset_name).Distinct().ToList();
        }

        /// <summary>
        /// 获取品牌名字
        /// </summary>
        /// <returns></returns>
        public List<string> GetBrandName()
        {
            List<Fixed_assets_info> instance = fixed_assets_infoService.QueryListByClauseAsync(f => f.IsDeleted == 0).Result;
            return instance.Select(x => x.Asset_brand).Distinct().ToList();
        }

        /// <summary>
        /// 获取存放地点
        /// </summary>
        /// <returns></returns>
        public List<string> GetStoragePlaceName()
        {
            List<Fixed_assets_info> instance = fixed_assets_infoService.QueryListByClauseAsync(f => f.IsDeleted == 0).Result;
            return instance.Select(x => x.Place_storage).Distinct().ToList();
        }


        /// <summary>
        /// 获取供应商名字
        /// </summary>
        /// <returns></returns>
        public List<string> GetSupplierName()
        {
            List<Fixed_assets_info> instance = fixed_assets_infoService.QueryListByClauseAsync(f => f.IsDeleted == 0).Result;
            return instance.Select(x => x.Asset_supplier).Distinct().ToList();
        }

    }
}
